﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLFacade.entities;
using BLFacade;

namespace ProjectUI
{
    public partial class CustomerForm : Form
    {
        private ICustomer customer;
        public CustomerForm()
        {
            InitializeComponent();
            btnContacts.Visible = false;
            btnEdit.Visible = false;
            btnProjects.Visible = false;
        }

        public CustomerForm(ICustomer customer)
        {
            InitializeComponent();
            this.customer = customer;
            grbxInfo.Enabled = false;
            txtName.Text = customer.Name;
            txtAddress.Text = customer.Address;
            txtDescription.Text = customer.Description;
            btnCreate.Text = "Submit";
            btnCreate.Enabled = false;
            this.Text = String.Format("Information about {0}", customer.Name);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnCreate.Enabled = grbxInfo.Enabled = !grbxInfo.Enabled;
        }

        private void txt_Validating(object sender, CancelEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(((TextBox)sender).Text))
            {
                errorFormProvider.SetError((TextBox)sender, "This field mustn't be empty");
            }
            else
            {
                errorFormProvider.SetError((TextBox)sender, "");
            }
        }

        private bool IsValid()
        {
            foreach (Control c in grbxInfo.Controls)
            {
                if (!String.IsNullOrEmpty(errorFormProvider.GetError(c))) return false;
            }
            return true;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (!IsValid()) return;
            if (btnCreate.Text.Equals("Create"))
            {
                FacadeHolder.DataFacade.CreateCustomer (txtName.Text, txtAddress.Text, txtDescription.Text);
            }
            if (btnCreate.Text.Equals("Submit"))
            {
                customer.Name = txtName.Text;
                customer.Address = txtAddress.Text;
                customer.Description = txtDescription.Text;
            }
            FacadeHolder.DataFacade.SaveChanges();
            this.Close();
        }

        private void btnContacts_Click(object sender, EventArgs e)
        {
            ContactListForm contacts = new ContactListForm(customer);
            contacts.MdiParent = this.MdiParent;
            contacts.Show();
        }

        private void btnProjects_Click(object sender, EventArgs e)
        {
            ProjectListForm projects = new ProjectListForm(customer);
            projects.MdiParent = this.MdiParent;
            projects.Show();
        }
    }
}
